<template>
  <div>
    <div class="echarts_cover">
      <!-- <h2 class="nav_title">可视化界面</h2> -->


<!-- 第一张可视化图表 -->
      <div class="center_cover">
        <div
          class="echarts1"
          id="main"
          style="width: 700px; height: 450px"
          v-if="series != []"
        ></div>
        <el-button
          class="btn1"
          @click="dialogFormVisible = true"
          v-if="series != []"
          >进入筛选</el-button
        >
      </div>
    </div>

    <!-- 筛选条件 -->
    <el-dialog title="筛选条件" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="form">
        <el-form-item label="请选择专业" :label-width="formLabelWidth">
          <el-cascader
            v-model="form.work_zhuanye"
            :options="options1"
          ></el-cascader>
        </el-form-item>

        <el-form-item label="请选择学历" :label-width="formLabelWidth">
          <el-select v-model="form.work_xueli" placeholder="请选择学历">
            <el-option label="本科" value="本科"></el-option>
            <el-option label="研究生" value="研究生"></el-option>
             <el-option label="博士" value="博士"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="请选择毕业时间" :label-width="formLabelWidth">
          <el-select v-model="form.work_biye_time" placeholder="请选择毕业时间">
            <el-option label="2020" value="2020"></el-option>
            <el-option label="2019" value="2019"></el-option>
            <el-option label="2018" value="2018"></el-option>
            <el-option label="2017" value="2017"></el-option>
            <el-option label="2016" value="2016"></el-option>
          </el-select>
        </el-form-item>

    
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm()">清 空</el-button>
        <el-button type="primary" @click="handleChange()">筛 选</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { filterEcharts } from "@/api/data";
export default {
  data() {
    return {
      options1: [],
      dialogFormVisible: false,
      form: {
        work_biye_time: "",
        work_xueli: "",
        work_jiuye: "",
        work_zhuanye: [],
      },
      formLabelWidth: "120px",
      series: [],
    };
  },
  mounted() {
    this.options1=JSON.parse(sessionStorage.getItem("structure"))
    let datalist = sessionStorage.getItem("echartsList");
    if (!datalist) {
      return this.handleChange();
    }
    this.series = JSON.parse(datalist);
    this.xuanran()   // 渲染第一张
  },
  methods: {
    // 条件搜索
    async handleChange() {
      this.dialogFormVisible = false;
      // 先查看条件
      let obj = {};
      for (var index in this.form) {
        if (!index || this.form[index] == "") {
          continue;
        }
        obj[index] = this.form[index];
      }

      // 判断 obj=={}
      var b = function () {
        for (var key in obj) {
          return false;
        }
        return true;
      };
      if (b() == true) {
        // obj=={}
        let datalist = sessionStorage.getItem("echartsList");
        if (!datalist) {
          // 表示本地没有数据，也就是说没经过预加载页面
          let data = await filterEcharts({
            work_biye_time: this.form.work_biye_time,
            work_xueli: this.form.work_xueli,
            work_jiuye: this.form.work_jiuye,
            work_zhuanye: this.form.work_zhuanye || [],
          });

          if (data.code == 200) {
            this.total = data.count;
            let length1 = data.data.length;
            // console.log("i",i)
            let obj1 = {
              name: "毕业人数",
              type: "line",
              // stack: "总人数",
              data: [0, 0, 0, 0, 0],
            };
            for (let i = 0; i < length1; i++) {
              for (let j = 0; j < data.data[i].data.length; j++) {
                obj1.data[j] += data.data[i].data[j];
                // console.log("j",j)
              }
            }
            data.data.push(obj1);
            this.series = data.data;
            this.xuanran();
          } else {
            this.$message.error(data.msg);
          }
        }
        this.series = JSON.parse(datalist);
        this.xuanran();
        return;
      }

      // obj!={}
      let data = await filterEcharts({
        work_biye_time: this.form.work_biye_time,
        work_xueli: this.form.work_xueli,
        work_jiuye: this.form.work_jiuye,
        work_zhuanye: this.form.work_zhuanye || [],
      });

      if (data.code == 200) {
        this.total = data.count;

        let length1 = data.data.length;
        // console.log("i",i)
        let obj1 = {
          name: "毕业人数",
          type: "line",
          // stack: "总人数",
          data: [0, 0, 0, 0, 0],
        };
        for (let i = 0; i < length1; i++) {
          for (let j = 0; j < data.data[i].data.length; j++) {
            obj1.data[j] += data.data[i].data[j];
            // console.log("j",j)
          }
        }
        data.data.push(obj1);
        this.series = data.data;
        this.xuanran();
      } else {
        this.$message.error(data.msg);
      }
    },

    // 清空条件
    resetForm() {
      this.form = {
        work_biye_time: "",
        work_xueli: "",
        work_jiuye: "",
        work_zhuanye: [],
      };
    },

    // 渲染第一张图表
    xuanran() {
      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(document.getElementById("main"));

      // 指定图表的配置项和数据
      var option = {
        title: {
          text: "就业升学一览",
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          data: ["就业", "灵活就业", "待就业", "升学", "毕业人数"],
        },
        grid: {
          left: "6%",
          right: "3%",
          bottom: "3%",
          containLabel: true,
        },
       toolbox: {
          show: true,
           orient: "vertical",
          left: "left",
          top: "center",
            feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ["line", "bar"] },
            restore: { show: true },
            saveAsImage: { show: this.$store.getters.isShow },
          },
        
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: ["2016", "2017", "2018", "2019", "2020"],
        },
        yAxis: {
          type: "value",
        },
        series: this.series,
      };
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },
  },
};
</script>

<style lang="scss" scoped>
.echarts_cover {
  height: calc(100vh -120px);
  text-align: center;
  padding: 50px 20px;
  position: relative;
}

.center_cover {
  display: flex;
  justify-content: center;
}

.btn1 {
  margin-left: 20px;
  // margin-top: 20px;
  height: 35px;
}
</style>